var loading=false;
var _loginUser=JSON.parse(localStorage.getItem("loginUser"));
var _loginAccount= _loginUser.userNo;
var _timeStamp=_loginUser.timeStamp;
var _appId = localStorage.getItem('reqConfItem').split(',')[0];
var token = { appId: _appId, device_name: 'testPad', loginUserCode: _loginAccount, timeStamp: _timeStamp, language: language, loginSysCode: 'ICM' }
console.log('token='+JSON.stringify(token));
var encdeToken = b.encode(encodeURI(JSON.stringify(token)));
console.log('encodedToken='+encdeToken);
bindLine();
bindDuanbie();

function bindLine(){
    var signData = { appId: token.appId, device_name: token.device_name, loginUserCode: token.loginUserCode, timeStamp: token.timeStamp, language: token.language, loginSysCode: token.loginSysCode };
    var sign = createSign(signData);
    $.ajax({
        url: icmpApi + '/common/getLineGroupList',
        data: JSON.stringify({ sign: sign }),
        headers: { token: encdeToken },
        contentType: "application/json",
        dataType: 'json',
        type: 'post',
        success: function (r) {
            console.log(r);
            if(r.error==0){
                if(r.data.length>0){
                    var _html='';
                    for(var i=0;i<r.data.length;i++){
                        _html+='<option value ="'+r.data[i].value+'">'+r.data[i].name+'</option>';
                    }
                    $('#_line').html(_html);
                }
            }
            else {
                alert(icmpApi + '/common/getLineGroupList 调用结果：' + r.msg);
            }
        },
        complete:function(){
            
        }
    });
}

function bindDuanbie(){
    var signData = { 
        type:0,//良率看板 使用大类type=0 AOI和WIP页面使用小类type=1
        appId: token.appId, 
        device_name: token.device_name, 
        loginUserCode: token.loginUserCode, 
        timeStamp: token.timeStamp, 
        language: token.language, 
        loginSysCode: token.loginSysCode 
    };
    var sign = createSign(signData);
    $.ajax({
        url: icmpApi + '/common/getStageList',
        data: JSON.stringify({ type:0, sign: sign }),
        headers: { token: encdeToken },
        dataType: 'json',
        contentType: "application/json",
        type: 'post',
        success: function (r) {
            console.log(r);
            if(r.error==0){
                if(r.data.length>0){
                    var _html='';
                    for(var i=0;i<r.data.length;i++){
                        _html+='<option value ="'+r.data[i].value+'">'+r.data[i].name+'</option>';
                    }
                    $('#_duan').html(_html);
                }
            }
            else {
                alert(icmpApi + '/common/getStageList 调用结果：' + r.msg);
            }
        },
        complete:function(){
            
        }
    });
}

var startTime,endTime;
$('#_startTime').change(function(){
    startTime=$(this).val().replace('T',' ');
    alert(startTime);
});
$('#_endTime').change(function(){
    endTime=$(this).val().replace('T',' ');
    alert(endTime)
});

$('#_queryBtn').click(function(){
    if(loading){
        alert('操作太频繁，请稍后再试');
        return;
    }
    var line=$('#_line').val();
    if(!line){
        alert('请选择线别');
        return;
    }
    console.log(line);
    var duan=$('#_duan').val();
    if(!duan){
        alert('请选择段别');
        return;
    }
    console.log(duan);
    if(!startTime){
        alert('请选择开始时间');
        return;
    }
    if(!endTime){
        alert('请选择结束时间');
        return;
    }
    var start = new Date(startTime.replace("-", "/").replace("-", "/"));
    var end = new Date(endTime.replace("-", "/").replace("-", "/"));
    if(start>end){
        alert('起止时间有误，请重新选择时间范围');
        return;
    }
    var signData = { 
        tartTime:startTime,endTime:endTime,stage:duan,line:line,
        appId: token.appId, device_name: token.device_name, loginUserCode: token.loginUserCode, timeStamp: token.timeStamp, language: token.language, loginSysCode: token.loginSysCode };
    var sign = createSign(signData);
    loading=true;
    $.ajax({
        url: icmpApi + '/report/getStationYieldList',
        data: JSON.stringify({ startTime:startTime,endTime:endTime,stage:duan,line:line,sign: sign }),
        headers: { token: encdeToken },
        dataType: 'json',
        type: 'post',
        success: function (r) {
            console.log(r);
            if(r.error==0){
                
            }
            else {
                alert(icmpApi + '/report/getStationYieldList 调用结果：' + r.msg);
            }
        },
        complete:function(){
            loading=false;
            alert('图表以静态数据展示');
            createChart(null,null);
        }
    });
});

function createChart(_firstData,_secondData){
    var chartDom = document.getElementById('_chart');
    var myChart = echarts.init(chartDom);
    var option;
    
    option = {
        
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            right:5,
            data: ['良率正常','良率异常','目标良率'],
            textStyle:{
                fontSize:'32px',
                color:'#ffffff',
    
            }
        },
        grid: {
            left: '3%',
            right: '3%',
            bottom: '3%',
            containLabel: true
        },
        calculable: true,
        xAxis: [
            {
                axisLabel:{
                    textStyle:{
                        fontSize:'32px',
                        color:'#ffffff'
                    }
                },
                type: 'category',
                data: ['AT06', 'AT08', 'AT10', 'AT13', 'AT20', 'AT31', 'AT37', 'AT39', 'AT42', 'AT55', 'AT60', 'AT64']
            }
        ],
        yAxis: [
            {
                type: 'value',
                axisLabel:{
                    textStyle:{
                        fontSize:'32px',
                        color:'#ffffff'
                    }
                }
            }
        ],
        series: [
            {
                name: '良率正常',
                type: 'bar',
                data: ['-', '-', '-', '-', '-', 76.7, 135.6, 162.2, '-', '-', '-', '-'],
                label: {
                    show: true,
                    position: 'top',
                    textStyle:{
                        fontSize:'32px',
                        color:'#ffffff'
                    }
                }
            },
            {
                name: '良率异常',
                type: 'bar',
                data: [2.0, 4.9, 7.0, 23.2, 25.6, '-', '-', '-', 32.6, 20.0, 6.4, 3.3],
                itemStyle:{
                    color: '#a90000'
                },
                label: {
                    show: true,
                    position: 'top',
                    textStyle:{
                        fontSize:'32px',
                        color:'#ffffff'
                    }
                },
                markLine: {
                    // data: [
                    //     {type: 'average', name: '平均值'}
                    // ]
                    symbol: "none",
                    /*symbol:"none",               //去掉警戒线最后面的箭头
                    label:{
                        position:"end"   ,       //将警示值放在哪个位置，三个值“start”,"middle","end"  开始  中点 结束
                    },*/
                    data: [{
                        silent: false,             //鼠标悬停事件  true没有，false有
                        lineStyle: {               //警戒线的样式  ，虚实  颜色
                            type: "dashed",
                            width:'5',
                            color: "#3398DB",
                        },
                        label: {
                            position: 'end',
                            formatter: ""
                        },
                        yAxis: 70         // 警戒线的标注值，可以有多个yAxis,多条警示线   或者采用   {type : 'average', name: '平均值'}，type值有  max  min  average，分为最大，最小，平均值
    
                    }]
                }
            }
        ]
    };
    option && myChart.setOption(option);
    myChart.on('click', function (params) {
        alert('点击了'+params.name+'，值为：'+params.value+'即将跳转至良率详情页');
        localStorage.setItem('param',JSON.stringify({startTime:startTime,endTime:endTime,line:$('#_line').val(),stationType:params.name}));
        location.href='liangLvDetail.html';
      })
}





